/*
 * Controller.java
 *
 * 02/11/2009
 */
package kg.sound.player.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFileChooser;

import kg.sound.player.Config;
import kg.sound.player.Model;


/**
 * @author fernando
 */
public class Controller {
	
	private static final Logger LOGGER = 
			Logger.getLogger(Controller.class.getName());
	
	private static final int PLAY_AS_STREAM = 1;
	private static final int PLAY_AS_CLIP = 2;
	
	private final Model model;
    private final Presenter presenter;
	
	public Controller(Model model, Presenter presenter) {
		this.model = model;
		this.presenter = presenter;
		
		presenter.addListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Controller.this.actionPerformed(e);
			}
		});
	}

    public Model getModel() {
        return model;
    }

    public Presenter getPresenter() {
        return presenter;
    }
    
    public View getView() {
    	return getPresenter().getView();
    }
    
    public void exit() {
    	getView().dispose();
    	Config.save();
    	System.exit(0);
    }

	private void addMedia() {
		MediaChooser mc = new MediaChooser();
		mc.setMultiSelectionEnabled(true);
		
		int ret = mc.showOpenDialog(getView());
		if (ret == JFileChooser.APPROVE_OPTION) {
			getModel().addMedia(mc.getSelectedFiles());
		}
	}

	private void removeClip() {
		getModel().removeMedia(getPresenter().getSelectedFilesInPlaylist());
	}

	private void clearPlaylist() {
		getModel().clearPlaylist();
	}

	private void play(int type) {
		Loading loading = new Loading();
		loading.setVisible(true);
		
		File[] files = getPresenter().getSelectedFilesInPlaylist();
		for (File file : files) {
			try {
				
				if (type == PLAY_AS_STREAM) {
					getModel().playStream(file);
				} else {
					getModel().playClip(file);
				}
				
			} catch (Throwable th) {
				LOGGER.log(Level.SEVERE,
						"An exception has ocurred while trying to play "
								+ "the media file: " + file, th);
				
				String message = String.format(
						"Could not play the media file: %s\n" +
						"    Cause: %s", 
						file.getName(), th.getLocalizedMessage());
				Notification.getInstance().addMessage(message);
			}
		}
		
		loading.dispose();
	}

	private void stop() {
		File[] files = getPresenter().getSelectedFilesInPlayingNow();
		for (File file : files) {
			try {
				getModel().stop(file);
				
			} catch (Throwable th) {
				LOGGER.log(Level.SEVERE,
						"An exception has ocurred while trying to stop playing "
								+ "the media file: " + file, th);
				
				String message = String.format(
						"Could not stop playing the media file: %s\n" +
						"    Cause: %s", 
						file.getName(), th.getLocalizedMessage());
				Notification.getInstance().addMessage(message);
			}
		}
	}
    
    public void actionPerformed(ActionEvent e) {
    	if (e.getActionCommand().equals(Presenter.EXIT))
    		exit();
    	if (e.getActionCommand().equals(Presenter.ADD_MEDIA))
    		addMedia();
    	if (e.getActionCommand().equals(Presenter.REMOVE_MEDIA))
    		removeClip();
    	if (e.getActionCommand().equals(Presenter.CLEAR_PLAYLIST))
    		clearPlaylist();
    	if (e.getActionCommand().equals(Presenter.PLAY_MEDIA))
    		play(PLAY_AS_STREAM);
    	if (e.getActionCommand().equals(Presenter.PLAY_MEDIA_AS_CLIP))
    		play(PLAY_AS_CLIP);
    	if (e.getActionCommand().equals(Presenter.STOP_MEDIA))
    		stop();
    }
}
